<script type="text/javascript" src="js/services.js"></script>
<link type="text/css" rel="stylesheet" href="css/services.css"/>
<style type="text/css">
    .filter{
        width: 200px;
        height: 28px;
        overflow: hidden;
        background: url("images/dropdown.png") no-repeat right #ddd;
        border: 0;
        float: left;
        margin-bottom: 10px;
    }

    .filter select{
        background: transparent;
        width: 225px;
        line-height: 1;
        padding: 5px;
        border: none;
        border-radius: 1px;
        -webkit-appearance: none;
    }

    #frm_dm{
        margin-top: 10px;
        margin-bottom: 30px;
        padding-bottom: 20px;
    }
	.type1{
		float:none;
		width:800px;
	}
	td.tdtype1{
		 background-color: #CCCCCC;
		border-bottom: 1px dashed;
		color: #000000;
		font-weight: bold;
		text-align: right;
	}
	.imageButtonSmall{float:none;}
</style>
<div class="container type1">
		<h1>Thống kê</h1>
<script type="text/javascript">
 $(function() {
<?php if($selected!="D"){?>
	$(".ngay").css("display","none");
<?php } ?>
<?php if($selected=="D" || $selected == "Y"){?>
	$(".thang").css("display","none");
<?php } ?>
<?php if($selected=="D" || $selected=="W" || $selected=="M" ){?>
	$(".nam").css("display","none");
<?php } ?>
	$(".thang").click(function(){
		$(".ngay").hide("slow");
		var id = $(this).attr("id");
		if($("."+id).css("display") == "none")
			$("."+id).show("slow");
		else $("."+id).hide("slow");
	});
	$(".nam").click(function(){
		$(".thang").hide("slow");
		$(".ngay").hide("slow");
		var id = $(this).attr("id");
		if($("."+id).css("display") == "none")
			$("."+id).show("slow");
		else $("."+id).hide("slow");
	});
	$( "#statistics_dt_from" ).datepicker({
			showOn: "button",
			changeYear:true,
			changeMonth:true,
			buttonImage: "<?php echo base_url();?>resources/images/calendar.gif",
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy'
		}).change(function(){
			
		});
	$( "#statistics_dt_to" ).datepicker({
			showOn: "button",
			changeYear:true,
			changeMonth:true,
			buttonImage: "<?php echo base_url();?>resources/images/calendar.gif",
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy'
		}).change(function(){
			
		});
	$("select[name='theo']").change(function(){
		var type = $("select[name='theo'] option:selected").val();
		if(type=="CM"){
			$('#statistics_dt').css("display","none");
                        $('.m').css("display","none");
			$('.cm').css("display","block");
		}else{
			$('#statistics_dt').css("display","inline");
                        $('.m').css("display","inline");
			$('.cm').css("display","none");
		}
	});
	$("select[name='theo']").change();
 });
 
</script>
<?php echo form_open(current_url(), 'id="frm_dm"'); ?>	
<div style="margin-bottom: 25px">
	<span style="margin: 0px auto; width: 295px;">
		<span style="display:inline; margin-top: 5px; position: relative;float:left;">Chọn:</span>
		<div class="filter">
			<?php echo $combobox_cats; ?>
		</div>
	</span>
	<span class="m" style="margin: 0px auto; width: 295px;">
    <input id="statistics_dt" type="text" name="statistics_dt" value="<?php echo $time; ?>"  />
	</span>
    <span class="cm" style="display:inline; position: relative;">
        From<input id="statistics_dt_from" type="text" name="statistics_dt_from" value="<?php echo $time_from; ?>"  />
        To<input id="statistics_dt_to" type="text" name="statistics_dt_to" value="<?php echo $time_to; ?>"  />
    </span>
</div>
<div style="clear:both;"></div>
<input type="image" id="button_submit" name="button_submit" value="Liệt kê" class="imageButtonSmall" style="padding-left: 27px"/>

<?php echo form_close(); ?>
	
<table id="">
	<tr>
	  <?php
				$stt = 1;
				$ngay = 1;
				$thang = 1;
				echo "<th class='first border-right' style='border-right: solid 3px #fff;'>Stt</th>";
                 foreach ($rules as $i) {
					if (in_array($i['field_id'], $master_limited)) {
						if (isset($i['width']))
							$width = "width = '" . $i['width'] . "'";
						else
							$width = "";
						echo "<th $width class='".$i['class']."' style='".$i['style']."'>" . $i['label'] . "</th>";
					}
				}
            ?>
       
	</tr>
	 <?php
				$sumgia = 0;$sumtiendv=0;$sumgiam=0;$sumthanhtien=0;
				$tsumgia = 0;$tsumtiendv=0;$tsumgiam=0;$tsumthanhtien=0;
				$id=1;$tid=1;
                for( $i = 0; $i < count( $results ); $i++ ){
					$sumgia +=$results[ $i ]['Price'];
					$sumtiendv +=$results[ $i ]['Price_Serv'];
					$sumgiam +=$results[ $i ]['Discount_Percent'];
					$sumthanhtien +=$results[ $i ]['Money'];
					$ngay=date("d",$results[$i]['Create_Dt']);
					$thang=date("m",$results[$i]['Create_Dt']);
					$odd = '';
					if($i%2!=0) $odd='odd';
                    echo "<tr class='ngay$id ngay $odd'>";
							echo "<td>$stt</td>";
                            foreach( $rules as $item ){
                           	    if( $item[ 'field_id' ] != "Image_ID" && in_array( $item[ 'field_id' ], $master_limited ) ){
									$str="";
									if($item[ 'control' ] == "vnd") $str="class='money'";
         			                echo "<td $str>";
									if($item[ 'control' ] == "time")
										echo date("d/m/y",$results[ $i ][ $item[ 'field_id' ] ]);
									else if($item[ 'control' ] == "phieu" && $results[ $i ][ $item[ 'field_id' ] ])
										echo $results[ $i ][ $item[ 'field_id' ] ]." suat(tang ".$results[$i]['Tang']." suat)";
									else if($item[ 'control' ] == "suat" && $results[ $i ][ $item[ 'field_id' ] ]) 
										echo $results[ $i ][ $item[ 'field_id' ] ]." suat";
									else if($item[ 'control' ] == "vnd") 
										echo formatcurrency($results[ $i ][ $item[ 'field_id' ] ] * 1000);
									else echo $results[ $i ][ $item[ 'field_id' ] ];
									
            						echo "</td>";						
    					}?>
                         
                        <?php 
                            }
                      $stt++; 
                    echo "</tr>";
					
					if((count( $results )-1)==$i||($ngay!=date("d",$results[$i+1]['Create_Dt']))){
						$tsumgia += $sumgia;$tsumtiendv += $sumtiendv;$tsumgiam += $sumgiam;$tsumthanhtien += $sumthanhtien;
						?>
						<tr id='ngay<?php echo $id;?>' class="thang thang<?php echo $tid;?>">
							<td colspan="5" style="text-align:left;padding-left:30px;"><?php echo date("d/m/Y",$results[ $i ]['Create_Dt']);?></td>
							<td class="money"><?php echo formatcurrency($sumgia*1000);?></td>
							<td class="money"><?php echo formatcurrency($sumtiendv*1000);?></td>
							<td class="money"><?php echo formatcurrency($sumgiam*1000);?></td>
							<td class="money"><?php echo formatcurrency($sumthanhtien*1000);?></td>
						</tr>
						<?php					
						$sumgia = 0;$sumtiendv=0;$sumgiam=0;$sumthanhtien=0;$id++;$stt = 1;					
					}
					//nam
					if((count( $results )-1)==$i||($thang!=date("m",$results[$i+1]['Create_Dt']))){
						?>
						<tr id='thang<?php echo $tid;?>' class="nam">
							<td colspan="5" style="text-align:left;">Thang' <?php echo date("m",$results[ $i ]['Create_Dt']);?></td>
							<td class="money"><?php echo formatcurrency($tsumgia*1000);?></td>
							<td class="money"><?php echo formatcurrency($tsumtiendv*1000);?></td>
							<td class="money"><?php echo formatcurrency($tsumgiam*1000);?></td>
							<td class="money"><?php echo formatcurrency($tsumthanhtien*1000);?></td>
						</tr>
						<?php
						$tsumgia = 0;$tsumtiendv=0;$tsumgiam=0;$tsumthanhtien=0;$tid++;
					}
                }
            ?>
		
			<tr style="font-weight:bold; font-size:16px;">
				<td colspan="5" valign="right" class="tdtype1" style="border-top: 1px dashed;">Tổng cộng</td>
				<td colspan="4" class="money tdtype1" style="border-top: 1px dashed;"><?php echo formatcurrency($thanhtien*1000);?></td>
			</tr>
				<tr style="font-weight:bold; font-size:16px;">
				<td colspan="5" valign="right" class="tdtype1" >Tiền típ</td>
				<td colspan="4" class="money tdtype1"><?php echo formatcurrency($tientip*1000);?></td>
			</tr>
			</tr>
				<tr style="font-weight:bold; font-size:16px;">
				<td colspan="5" valign="right" class="tdtype1">Tiền dich vu</td>
				<td colspan="4" class="money tdtype1"><?php echo formatcurrency($sum_dv*1000);?></td>
			</tr>
			<tr style="font-weight:bold; font-size:16px;">
				<td colspan="5" valign="right" class="tdtype1" >Còn lại</td>
				<td colspan="4" class="money tdtype1"><?php echo formatcurrency($thanhtien*1000 - $tientip*1000 - $sum_dv*1000);?></td>
			</tr>
	</table>
	<h1>Tien dich vu</h1>
<table id="" style="width:400px;margin:0 auto;">
<tr>
<th class='first border-right' style='border-right: solid 3px #fff;'>Stt</th>
<th class='border-right' style='border-right: solid 3px #fff;'>Ten NV</th>
<th class='last' style=''>Tien dich vu </th>
</tr>

<?php 
$i=1;
foreach($tiendv as $item){
$odd = '';
if($i%2==0) $odd='odd';
?><tr class='<?php echo $odd;?>'>
<td class='c'><?php echo $i;?></td>
<td class=''><?php echo $item['Personal_ID'];?></td>
<td style="text-align:right;"><?php echo formatcurrency($item['Price_Serv']*1000);?></td>
</tr>
<?php $i++;} ?>
<tr>
<td colspan="2" class="tdtype1" style="border-top: 1px dashed;">Tong cong</td>
<td class="tdtype1" style="border-top: 1px dashed;ba"><?php echo formatcurrency($sum_dv*1000);?></td>
</tr>
</table>
</div>

